package net.starwifi.duobao.hfg.notify;

import cn.com.hongpay.client.utils.SignatureTools;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by STX on 2016/4/11.
 */
public class NotifyResponse {

    private NotifyInfo info;

    private List<NotifyTradeDetail> trade_details=new ArrayList<NotifyTradeDetail>();

    public NotifyResponse() {
    }

    public NotifyResponse(NotifyInfo info, NotifyTradeDetail trade_detail) {
        this.info = info;
        this.trade_details.add(trade_detail);
    }

    public String encode(String pfxFile,String pfxPwd,String privateKeyPwd) {
        String sign=makeSign(pfxFile,pfxPwd,privateKeyPwd);
        info.setSigned_msg(sign);
        String infoJson= JSONObject.toJSONString(info);
//        String detailJson=JSONObject.toJSONString(trade_details);
        StringBuffer sb=new StringBuffer();
        NotifyTradeDetail tradeDetail=trade_details.get(0);
        sb.append("[{\"partner_trade_no\":\""+tradeDetail.getPartner_trade_no()+"\"");
        sb.append(",\"trade_no\":\""+tradeDetail.getTrade_no()+"\"");
        sb.append(",\"result_code\":\""+tradeDetail.getResult_code()+"\"");
        sb.append("}]");
        String detailJson=sb.toString();
        String json="{ \"body\":{\"trans_details\":"+detailJson+"},\"info\":"+infoJson+"}";
        System.out.println("通知返回:"+json);
        return json;
    }

    public String makeSign(String pfxFile, String pfxPwd, String privateKeyPwd) {
        NotifyTradeDetail tradeDetail=trade_details.get(0);
        StringBuffer sb=new StringBuffer();
        sb.append("partner_id:"+info.getPartner_id());
        sb.append(";partner_verify_code:"+info.getPartner_verify_code());
        sb.append(";request_time:"+info.getRequest_time());
        sb.append(";trade_no:"+tradeDetail.getTrade_no());
        sb.append(";partner_trade_no:"+tradeDetail.getPartner_trade_no());
        sb.append(";result_code:"+tradeDetail.getResult_code()+";");

        SignatureTools st = new SignatureTools();
        String sign= st.sign(sb.toString(),pfxFile, pfxPwd, privateKeyPwd);
        return sign;
    }

    public NotifyInfo getInfo() {
        return info;
    }

    public void setInfo(NotifyInfo info) {
        this.info = info;
    }

    public List<NotifyTradeDetail> getTrade_details() {
        return trade_details;
    }

    public void setTrade_details(List<NotifyTradeDetail> trade_details) {
        this.trade_details = trade_details;
    }

}
